﻿$.MGJ.OriginsMaterial = [];

(function () {
    var _this = $.MGJ.OriginsMaterial;
    $.extend($.MGJ.OriginsMaterial, {
        // 新增
        Dialog_AddObj: null,
        Dialog_Add: function () {
            this.Dialog_AddObj = $.L.dialog({
                url: ACTION_URL("AddOriginsMaterial", "OriginsMaterial"),
                title: "新增素材库",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#MaterialForm").data('bootstrapValidator');
                            bootstrapValidator.validate();
                            if (bootstrapValidator.isValid()) {
                                $.MGJ.OriginsMaterial.Ajax_SaveMaterial();
                                _this.Dialog_AddObj.close();
                                $.L.msgSuccess("操作成功");
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            }
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },

        // 编辑
        Dialog_Edit: function (obj) {
            RootsPathAndID(obj)
            var id = $(obj).attr("id");
            $.MGJ.Common.loadurl(this, '/OriginsMaterial/EditOriginsMaterial?materialId=' + id);
        }
    });
})(jQuery);

(function () {
    var _this = $.MGJ.OriginsMaterial;
    $.extend($.MGJ.OriginsMaterial, {
        // 获取列表信息
        Ajax_GetOriginsMaterialList: function (pageIndex, where) {
            window.SearchKey = removeSpace($("#where").val());
            $.L.ajaxHandle({
                url: ACTION_URL("PageList", "OriginsMaterial"),
                data: {
                    where: window.SearchKey,
                    pageIndex: pageIndex,
                    pageSize: 10
                },
                success: function (response) {
                    $("#OMID").L_bindingDataList(response);
                }
            });
        },

        // 校验成功之后执行的保存数据的函数
        Ajax_SaveMaterial: function () {
            var data = $("#MaterialForm").serialize();
            $.L.ajaxHandle({
                url: ACTION_URL("MaterialSave", "OriginsMaterial"),
                data: $("#MaterialForm").serialize(),
                success: function (result) {
                    if (result > 0) {
                        _this.Ajax_GetOriginsMaterialList(1);
                        _this.Dialog_AddListObj.close();
                        $.L.msgSuccess("操作成功");
                    }
                }
            });
        },

        // 动态绑定图标
        BindMaterialLogo: function (val) {
            switch (val) {
                case 0:
                    return "div_icon Icon_Common";
                case 1:
                    return "div_icon Icon_Gold";
                case 2:
                    return "div_icon Icon_Platinum";
                case 3:
                    return "div_icon Icon_Diamond";
                default:
                    break;
            }
        },

        // 删除
        Ajax_DelMaterial: function (obj) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("ID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DelMaterial", "OriginsMaterial") + "?id=" + id,
                    data: { id: id },
                    success: function (response) {
                        if (response > 0) {
                            $.L.msgSuccess("操作成功！");
                            _this.Ajax_GetOriginsMaterialList(1);

                        }
                    }
                });
            })
        },

        // 编辑素材名称(span)
        ChangeNameDiv: function (_this) {
            var _thisNub = $(".ChangeNameDiv").index($(_this).parents(".ChangeNameDiv"))

            $(".ChangeNameDiv").eq(_thisNub).hide();
            $(".ChangeNameInp").eq(_thisNub).val($.trim($(".ChangeNameDiv").eq(_thisNub).text())).show();
            $(".ChangeNameInp").eq(_thisNub).focus();
        },

        // 编辑素材名称(input)
        ChangeNameInp: function (_this) {
            var _thisNub = $(".ChangeNameInp").index($(_this))
            var materialName = $.trim($(".ChangeNameInp").eq(_thisNub).val());
            var materialId = $(_this).attr("id");
            if (materialName != "") {
                $(".ChangeNameInp").eq(_thisNub).hide();
                $(".ChangeNameDiv").eq(_thisNub).show().children(".ChangeNameDivChild").text($.trim($(".ChangeNameInp").eq(_thisNub).val()));
                //素材名称修改回调
                $.L.ajaxHandle({
                    url: ACTION_URL("UpdateMaterialName", "OriginsMaterial"),
                    data: {
                        materialName: materialName,
                        materialId:materialId
                    },
                    success: function (data) {
                        //console.log(data);
                    }
                });
            } else {
                $.L.msgError("素材名称不能为空")
            }
        },

        // 预览(Common.js-->select)
        ChangeModel: function (_this) {
            if (_this.value == "iphone7") {
                $("#iphoneViews").css("width", "375px")
            } else {
                $("#iphoneViews").css("width", "414px")
            }
        }
    });
})(jQuery);